package com.fy.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fy.core.vo.ApiRest;
import com.fy.sys.entity.DictData;
import com.fy.sys.mapper.DictDataMapper;
import com.fy.sys.service.DictDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典数据表 服务实现类
 * </p>
 *
 * @author hxd
 * @since 2023-06-14
 */
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData> implements DictDataService {


    @Override
    public List<DictData> findAll(String dictType) {

        return this.baseMapper.findByDictType(dictType);
    }

    @Override
    public ApiRest add(DictData dictData) {

        dictData.setStatus("0");
        dictData.setCreateTime(new Date());
        this.baseMapper.insert(dictData);

        return ApiRest.success("添加成功","");
    }

    @Override
    public ApiRest updateByStatus(Long dictCode) {
        DictData dictData = this.baseMapper.selectById(dictCode);

        UpdateWrapper<DictData> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(DictData::getDictCode,dictCode);

        if (dictData.getStatus().equals("0")) {
            dictData.setStatus("1");
        }else{
            dictData.setStatus("0");
        }

        this.baseMapper.update(dictData,updateWrapper);

        return null;
    }

    @Override
    public ApiRest deleteDictData(Long dictCode) {
        this.baseMapper.deleteById(dictCode);
        return ApiRest.success("Delete success","");
    }

    @Override
    public ApiRest batchDelete(List<DictData> dictDatas) {

        List<Long> codes = dictDatas.stream().map(item -> item.getDictCode()).collect(Collectors.toList());

        for (Long code : codes) {
            this.baseMapper.deleteById(code);
        }

        return ApiRest.success("批量删除字典数据");
    }
}
